<template>
  <div class="user-add-or-update">
    <el-drawer :title="this.rowData ? '编辑用户' : '新增用户'" :visible.sync="drawer" custom-class="demo-drawer" direction="rtl" size="88%">
      <div class="demo-drawer__content">
        <div :style="cardHeightStyle" class="rightScrollNone">
          <el-form size="mini" :model="dataForm" :rules="rules" ref="vendorRuleForm">
            <el-row>
              <el-col :span="24">
                <el-form-item :label-width="formLabelWidth" label="用户名称：" prop="name">
                  <el-input autocomplete="off" type="text" v-model="dataForm.name"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label-width="formLabelWidth" label="手机号：" prop="phone">
                  <el-input autocomplete="off" type="text" v-model="dataForm.phone"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label-width="formLabelWidth" label="角色：" prop="rolesId">
                  <el-select placeholder="请选择" style="width: 100%" v-model="dataForm.rolesId">
                    <el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in roleOptions"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label-width="formLabelWidth" label="登录账号：" prop="account">
                  <el-input autocomplete="off" v-model="dataForm.account"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label-width="formLabelWidth" label="登录密码：" prop="password">
                  <el-input autocomplete="off" v-model="dataForm.password" type="password"></el-input>
                </el-form-item>
              </el-col>
              <!--              <el-col :span="24">-->
              <!--                <el-form-item :label-width="formLabelWidth" label="数据权限：" prop="status">-->
              <!--                  <el-select placeholder="请选择" style="width: 100%;" v-model="dataForm.data_power">-->
              <!--                    <el-option-->
              <!--                      :key="item.value"-->
              <!--                      :label="item.label"-->
              <!--                      :value="item.value"-->
              <!--                      v-for="item in dataPowerList">-->
              <!--                    </el-option>-->
              <!--                  </el-select>-->
              <!--                </el-form-item>-->
              <!--              </el-col>-->
              <!--              <el-col :span="24">-->
              <!--                <el-form-item :label-width="formLabelWidth" label="显示未分配客户：" prop="status">-->
              <!--                  <el-select placeholder="请选择" style="width: 100%;" v-model="dataForm.if_show">-->
              <!--                    <el-option-->
              <!--                      :key="item.value"-->
              <!--                      :label="item.label"-->
              <!--                      :value="item.value"-->
              <!--                      v-for="item in statusOptions">-->
              <!--                    </el-option>-->
              <!--                  </el-select>-->
              <!--                </el-form-item>-->
              <!--              </el-col>-->
            </el-row>
          </el-form>
        </div>
        <div class="demo-drawer__footer">
          <el-button size="mini" @click="cancelForm">取 消</el-button>
          <el-button size="mini" :loading="loading" @click="dataFormSubmit" type="primary">确定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import city from './city.json'
import { addUserApi, editUserApi, nameCheckApi } from '@/api/system/index.js'
import { isMobile, notHavNumAndSymbol, isPassword } from '@/utils/validate.js'
import { organizationListApi } from '../../../api/system'

export default {
  data() {
    var validaName = (rule, value, callback) => {
      if (value && !notHavNumAndSymbol(value)) {
        callback(new Error('用户名只能为汉字或字母'))
      } else {
        callback()
      }
    }
    var validaMobile = (rule, value, callback) => {
      if (value && !isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    var validaPassword = (rule, value, callback) => {
      if (value && !isPassword(value)) {
        callback(new Error('密码6~20位 必须含大写字母、小写字母、数字'))
      } else {
        callback()
      }
    }
    var validaIsPassword = (rule, value, callback) => {
      if (value && this.dataForm.password != value) {
        callback(new Error('两次输入密码值不相等'))
      } else {
        callback()
      }
    }
    return {
      cityOptions: city,
      schooloptions: [],
      timeOutCount: 0,
      showNumInput: false,
      showRefBtn: false,
      refNum: 0,
      dataForm: {
        id: '',
        name: '', // 用户名
        account: '', //手机号
        rolesId: '', // 角色
        password: '', //密码
        phone: '',
      },
      rowData: null,
      //cityOptions: citys.options,
      formLabelWidth: '150px',
      loading: false,
      drawer: false,
      dataPowerList: [
        { label: '仅自己', value: 1 },
        { label: '机构', value: 2 },
        { label: '机构及以下', value: 3 },
      ],
      statusOptions: [
        { label: '显示', value: 1 },
        { label: '不显示', value: 2 },
      ],
      roleOptions: [],
      rules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { validator: validaName, trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validaMobile, trigger: 'blur' },
        ],
        account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        organizationId: [{ required: true, message: '请选择组织机构', trigger: 'blur' }],
        rolesId: [{ required: true, message: '请选择角色', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validaPassword, trigger: 'blur' },
        ],
      },
      organizationArr: [],
    }
  },
  mounted() {},
  computed: {
    cardHeightStyle() {
      let documentClientHeight = document.body.clientHeight
      const height = documentClientHeight - 80 - 80
      return {
        height: height + 'px',
        'overflow-y': 'scroll',
        'padding-right': '20px;',
      }
    },
  },
  methods: {
    areaChangeHandle() {
      this.$refs.cascaderHandle.dropDownVisible = false
      const zone = this.dataForm.zoneList.join('/')
      this.dataForm.zone = zone
    },
    showCodeInputHandle() {
      // this.dataForm.is_email = 0
      this.refNum -= 1
      this.showNumInput = true
      this.showRefBtn = false
      this.timeOutCount = setTimeout(this.bandingEmailHandle, 1000) //保存定时器的句柄，方便清除定时器
    },

    reportAdd(params) {
      if (this.dataForm.password.trim() == '') {
        params.password = 'Aa123456'
      }
      return addUserApi(params)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success('保存成功')
          }
        })
        .catch((res) => {
          this.$message.error('保存失败')
        })
    },
    reportUpdate(params) {
      return editUserApi(params)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success('保存成功')
          }
        })
        .catch((res) => {
          this.$message.error('保存失败')
        })
    },
    dataFormSubmit() {
      this.$refs.vendorRuleForm.validate((flag) => {
        if (flag) {
          const data = this.dataForm
          // 根据角色ID,获角色其他信息
          const organizationId_ = data.organizationId
          data.organizationId = JSON.stringify(organizationId_)
          var request = this.rowData ? this.reportUpdate(data) : this.reportAdd(data)
          request
            .then((d) => {
              this.cancelForm()
              this.$emit('refreshList')
            })
            .catch((d) => {
              this.cancelForm()
            })
        }
      })
    },
    cancelForm() {
      this.cleanInfo()
      this.loading = false
      this.drawer = false
    },
    open(row, role) {
      if (this.drawer) {
        return
      }
      this.roleOptions = role
      this.rowData = row || null
      this.drawer = true
      this.openHook()
      // this.getshcoolselect()
    },
    clearData() {
      this.$refs.vendorRuleForm.resetFields()
    },
    cleanInfo() {
      clearTimeout(this.timeOutCount)
    },
    openHook() {
      this.$nextTick((a) => {
        this.showNumInput = false
        this.refNum = 60 * 5 + 1
        this.clearData()
        if (this.rowData) {
          // 修改
          for (let key in this.dataForm) {
            this.dataForm[key] = this.rowData[key]
          }
        } else {
          this.loading = false
        }
      })
    },
  },
}
</script>
<style lang="scss">
.user-add-or-update {
  .el-tag {
    position: relative;
    right: 5px;
    z-index: 1;
    height: 40px;
    line-height: 40px;
    border-radius: 0 5px 5px 0;
    padding-left: 15px;
  }

  .my-email {
    .el-input__inner {
      border-radius: 5px 0 0 5px;
    }
  }
}

/*

  */

.fileInput {
  padding-right: 0;
}
</style>
